﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SettlersEngine
{
    internal class Building : AutoPositionTranformable, IVisualSizedObject
    {
        private AnimatedElement m_Element;

        public IBuildableObject Buildable { get; private set; }
        public int VisualHeight { get { return Config.VisualHeight; } }
        public int VisualWidth { get { return Config.VisualWidth; } }
        public System.Drawing.Rectangle LockedBounds { get { return Config.LockedBounds; } }
        public BuildingConfig Config { get { return Buildable.Config; } }

        public Building(Exported.GameInstance inInstance, IBuildableObject inBuildable)
            : base(inInstance, inBuildable)
        {
            Buildable = inBuildable;

            m_Element = new AnimatedElement()
            {
                Source = Config.AnimationClass,
            };

            Content = m_Element;
            m_Element.Player.OnStop += Player_OnStop;

            inBuildable.OnProductionStarted += OnProductionStarted;
        }

        void OnProductionStarted(IBuildableObject inSender)
        {
            try
            {
                m_Element.Play("Step");
            }
            catch
            {
                Player_OnStop(null);
            }
        }

        void Player_OnStop(AnimationPlayer inPlayer)
        {
            if (!Buildable.IsProducing)
                return;

            Int64 elapsed = Buildable.ProductionElapsedMillis;

            if (elapsed < Buildable.Config.ProductionTimeMillis)
            {
                WPFPrecisionSchedule.QueueTask((a, b) =>
                {
                    Buildable.CompleteProduction();
                }, Buildable.Config.ProductionTimeMillis - elapsed);
            }
            else
            {
                Buildable.CompleteProduction();
            }
        }
    }
}
